class Solution
{
public:
    vector<vector<int>> permuteUnique(vector<int> &nums)
    {
        vector<vector<int>> result;
        setPerm(nums, result, 0, nums.size());
        return result;
    }

    void setPerm(vector<int> &nums, vector<vector<int>> &result, int pos, int n)
    {
        if (pos == n)
        {
            result.push_back(nums);
            return;
        }
        unordered_set<int> occurred;
        for (int i = pos; i < n; ++i)
        {
            if (occurred.count(nums[i]))
            {
                continue;
            }
            swap(nums[pos], nums[i]);
            occurred.insert(nums[pos]);
            setPerm(nums, result, pos + 1, n);
            swap(nums[pos], nums[i]);
        }
    }
};